<template>
  <el-input
    class="input"
    :type="config.type"
    :disabled="isReadonly"
    v-model="inputVal"
    @change="change"
    :placeholder="isReadonly ? '' : `请输入${config.name}`"
    clearable
    @clear="update(null)"
    :maxlength="config.maxlength ? config.maxlength : 50"
    show-word-limit
  >
    <el-button
      v-if="config.icon"
      slot="append"
      :icon="config.icon.class"
      @click="iconClick"
    ></el-button>
  </el-input>
</template>
<script>
import Inject from "../../../mixin/inject";
export default {
  mixins: [Inject],
  props: {
    isReadonly: {
      type: Boolean,
      default: false,
    },
    formData: Object,
    config: {
      type: Object,
      default: () => Object.create(null),
    },
  },
  data() {
    return {
      inputVal: null,
    };
  },
  computed: {
    curVal() {
      return this.formData[this.config.prop] || "";
    },
  },
  watch: {
    curVal: {
      immediate: true,
      deep: true,
      handler(val) {
        this.inputVal = val;
      },
    },
  },
  mounted() {},
  methods: {
    iconClick() {
      if (
        this.config.icon.click &&
        this.$CD[this.config.icon.click] instanceof Function
      ) {
        this.$CD[this.config.icon.click]();
      }
    },
    update(val) {
      //this.formData[this.config.prop] = val;
      this.$set(this.formData, this.config.prop, val);
      this.$emit("update:formData", this.formData);
      this.$emit("change");
    },

    change(val) {
      this.inputVal = val;
      this.update(val);
    },
  },
};
</script>
<style lang="scss" scoped></style>
